package org.jeecg.modules.easyai.keywordCapture.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.easyai.keywordCapture.entity.KeywordCapture;
import org.jeecg.modules.easyai.keywordCapture.mapper.KeywordCaptureMapper;
import org.jeecg.modules.easyai.keywordCapture.service.IKeywordCaptureService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.wlld.entity.KeyWordForSentence;
import org.wlld.naturalLanguage.languageCreator.CatchKeyWord;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Description: 关键词类别捕获
 * @Author: jeecg-boot
 * @Date:   2024-07-23
 * @Version: V1.0
 */
@Service
public class KeywordCaptureServiceImpl extends ServiceImpl<KeywordCaptureMapper, KeywordCapture> implements IKeywordCaptureService {

    public static List<CatchKeyWord> catchKeyWordList = new ArrayList<CatchKeyWord>();

    //初始化三次因为过根据样本提取出三个关键词
    public void init() {
        for (int i = 0; i < 3; i++) {
            CatchKeyWord catchKeyWord =  new CatchKeyWord();
            catchKeyWord.setProTh(-10);
            catchKeyWordList.add(catchKeyWord);
        }
    }

    @Override
    public JSONObject identification(String sentence) throws Exception {
        init();
        List<KeyWordForSentence> keyWordTimes = new ArrayList<KeyWordForSentence>();
        List<KeyWordForSentence> starts = new ArrayList<KeyWordForSentence>();
        List<KeyWordForSentence> ends = new ArrayList<KeyWordForSentence>();
        LambdaQueryWrapper<KeywordCapture> queryWrapper = new LambdaQueryWrapper<>();
        List<KeywordCapture> keywordCaptureList = this.baseMapper.selectList(queryWrapper);
        keywordCaptureList.forEach(keywordCapture -> {
            KeyWordForSentence time = new KeyWordForSentence();
            KeyWordForSentence start = new KeyWordForSentence();
            KeyWordForSentence end = new KeyWordForSentence();
            time.setSentence(keywordCapture.getSentence());
            time.setKeyWord(keywordCapture.getStartTime());
            start.setSentence(keywordCapture.getSentence());
            start.setKeyWord(keywordCapture.getStartLocation());
            end.setSentence(keywordCapture.getSentence());
            end.setKeyWord(keywordCapture.getEndLocation());
            keyWordTimes.add(time);
            starts.add(start);
            ends.add(end);
        });
        catchKeyWordList.get(0).study(keyWordTimes);
        catchKeyWordList.get(1).study(starts);
        catchKeyWordList.get(2).study(ends);
        Set<String> time = catchKeyWordList.get(0).getKeyWord(sentence);
        Set<String> start = catchKeyWordList.get(1).getKeyWord(sentence);
        Set<String> end = catchKeyWordList.get(2).getKeyWord(sentence);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("time", time);
        jsonObject.put("start", start);
        jsonObject.put("end", end);
        jsonObject.put("answer", "【时间: " + String.join("", time) + "  出发地: " + String.join("", start)+"  目的地: " + String.join("", end)+"】");
        return jsonObject;
    }
}
